home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10977 < prev    next >
Encoding:
Text File  |  1996-08-05  |  7.4 KB  |  366 lines

  1. Path: earth.superlink.net!usenet
  2. From: Michael Rizzo <rizzom@mars.superlink.net>
  3. Newsgroups: comp.lang.c++
  4. Subject: Strings, Templates--> confusion
  5. Date: Mon, 11 Mar 1996 23:22:42 -0500
  6. Organization: SuperNet Inc. (908) 828-8988
  7. Message-ID: <3144FC12.65B8@mars.superlink.net>
  8. NNTP-Posting-Host: ez5.superlink.net
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13.  
  14. Hi all,
  15.  
  16.     Once again I am posting a novice question and hoping for your 
  17. wisdom to help me.  Here it goes:
  18.     I have defined my own string class called string, as well as a 
  19. 'kind of' array template class called MemManager.  Then I have a third 
  20. class called VMPROC that contains two instances of the MemManager 
  21. template class  one being a string instantiation and one being an 
  22. integer instantion.  The integer instantiation  works just fine, but...
  23. the string instantion is driving my crazy.  Any time I store a string to 
  24. any one of the cells in the MemManager<string> array,  it assigns that 
  25. value to all of the cells.  It seems like all of the cells are pointing 
  26. to the same memory location or something like that.  I think it's 
  27. something in my string class, but I have not been able to figure out 
  28. what it is.  I have included the code (it is not in very good form at 
  29. the moment).  BTW, I have tried it with Visual C++ 4.0 compiling as a 
  30. console app, and with DJGPP v2.  I know this is alot to ask, but I am at 
  31. wits end.  I just can't figure out whats wrong.  Thanks in advance to 
  32. any who can spare some time to help.  my e-mail is: 
  33. rizzom@mars.superlink.net
  34.  
  35. --Mike
  36.  
  37. Source code listing:
  38.  
  39. File string.h
  40. #ifndef IOSTREAM_H_
  41. #define IOSTREAM_H_
  42. #include <iostream.h>
  43. #endif
  44.  
  45. #ifndef STDLIB_H_
  46. #define STDLIB_H_
  47. #include <stdlib.h>
  48. #endif
  49.  
  50. //#include <new.h>
  51. #ifndef STRING_H_
  52. #define STRING_H_
  53. #include <string.h>
  54. #endif
  55.  
  56.  
  57. class string
  58. {
  59.     int size;
  60.     char *str;
  61. public:
  62.     string (const string&);
  63.     string (const char*);
  64.     string (int);
  65.     string(); 
  66.     string (const char*, int);
  67.     ~string();
  68.     int GetLen() {return size;};
  69.     
  70.     char& operator[](int);
  71.     int operator!() const;
  72.     string& operator+=(const string&);
  73.     string& operator=(char *);
  74.     friend ostream& operator<<(ostream&, string&);
  75. };
  76.  
  77.  
  78. File string.cpp
  79. #ifndef MY_STRING_H_
  80. #define MY_STRING_H_
  81. #include "string.h"
  82. #endif
  83.  
  84. string::string()
  85. {
  86.     size = 0;
  87.     str = new char[size + 1];
  88.     str[0] = NULL;
  89. }
  90.  
  91. string::string (const string& s)
  92. {
  93.     size = s.size;
  94.     str = new char[size + 1];
  95.     strcpy(str, s.str);
  96. }
  97.  
  98. string::string(const char *s)
  99. {
  100.     size = strlen(s);
  101.     str = new char[size+1];
  102.     strcpy(str, s);
  103. }
  104.  
  105.  
  106. string::string(const char *cptr, int sz)
  107. {
  108.     size = sz;
  109.     str = new char[size +1];
  110.     strcpy(str, cptr);
  111. }
  112.  
  113. string::string(int sz)
  114. {
  115.     size = sz;
  116.     str = new char[size + 1];
  117.             
  118. }
  119.  
  120. string::~string()
  121. {
  122.     delete str;
  123.     //cout << "called ~string" << endl;
  124. }
  125.  
  126.  
  127. char& string::operator[](int ind)
  128. {
  129.     if (ind >= 0 && ind < size)
  130.         return str[ind];
  131.     else
  132.     {
  133.         cout << "Subscript out of bounds" << endl;
  134.         exit(-1);
  135.         return  str[ind];
  136.     }
  137. }
  138.  
  139. string& string::operator+=(const string& x1)
  140. {
  141.     size += x1.size;
  142.     char *tmp = new char[size+1];
  143.     strcpy(tmp,str);
  144.     strcat(tmp,x1.str);
  145.     delete str;
  146.     str = tmp;
  147.     return *this;
  148. }    
  149.  
  150.  
  151. int string::operator!() const
  152. {
  153.     return size == 0;
  154. }
  155.  
  156. string& string::operator=(char *s)
  157. {
  158.     cout << "= invoked::  ";
  159.     size = strlen(s);
  160.     delete str;
  161.     str = new char[size+1];
  162.     strcpy(str,s);
  163.     return *this;
  164. }
  165.  
  166. //Friend function defs
  167. ostream& operator<<(ostream& os, string& tmp)
  168. {
  169.     return os << tmp.str;
  170. }
  171.  
  172. File memman.h
  173. #ifndef IOSTREAM_H_
  174. #define IOSTREAM_H_
  175. #include <iostream.h>
  176. #endif
  177.  
  178. #ifndef STDLIB_H_
  179. #define STDLIB_H_
  180. #include <stdlib.h>
  181. #endif
  182.  
  183. const int MemSize = 100;
  184.  
  185. template <class T>
  186. class MemManager
  187. {
  188.     void init(const T*, int);
  189.  
  190.     int size;
  191.     T *mem;
  192.  
  193.  public:
  194.     MemManager(int sz=MemSize) {mem = new T[size=sz];};
  195.     MemManager(const T *m, int sz) {init(m,sz);};
  196.     MemManager(const MemManager &M) {init (M.mem, M.size);};
  197.     ~MemManager() {delete [] mem;};
  198.  
  199.     int GetSize() {return size;};
  200.     T& Fetch(int ind) {return mem[ind];};
  201.     void Store(T& item, int ind) {mem[ind] = item;};
  202.     T& operator[] (int ind) {return mem[ind];};
  203.     MemManager& operator=(const MemManager&);
  204. };
  205.  
  206.  
  207. File Memman.h
  208. #ifndef MEMMAN_H_
  209. #define MEMMAN_H_
  210. #include "MemMan.h"
  211. #endif
  212.  
  213. template <class T>
  214. void MemManager<T>::init(const T *memman, int sz)
  215. {
  216.     size = sz;
  217.     mem = new T[size];
  218.  
  219.     for (int i=0; i<size;++i)
  220.         mem[i] = memman[i];
  221. }
  222.  
  223. template <class T>
  224. MemManager<T>& MemManager<T>::operator=(const MemManager &m)
  225. {
  226.     if (this == &m) return *this;
  227.     delete [] mem;
  228.     init(m.mem, m.size);
  229.     return *this;
  230. }
  231.  
  232.  
  233. File main.cpp
  234. #include "memman.h"
  235. #include "string.h"
  236.  
  237. #ifndef IOSTREAM_H_
  238. #define IOSTREAM_H_
  239. #include <iostream.h>
  240. #endif
  241.  
  242. #ifndef STDLIB_H_
  243. #define STDLIB_H_
  244. #include <stdlib.h>
  245. #endif
  246.  
  247. #ifndef STDIO_H_
  248. #define STDIO_H_
  249. #include <stdio.h>
  250. #endif
  251.  
  252. enum STATE {IDLE = 0, BUSY};
  253. enum boolean {FALSE=0, TRUE};
  254.  
  255. class VMPROC
  256. {
  257.     int AC, PC;  //accum. program counters
  258.     string IR; //instruction register
  259.     STATE state; //machine state
  260.     MemManager<int> dataMem;  //date memory array
  261.     MemManager<string> instMem;  //instruction memory array
  262.  
  263.  public:
  264.      VMPROC() {cout << "VMPROC instantiated" << endl;};
  265.     ~VMPROC() {cout << "VMPROC destroyed" << endl;};
  266.     STATE GetState() {return state;};
  267.     boolean Init();
  268.     void DisplayMem();
  269. };
  270.  
  271. boolean VMPROC::Init()
  272. {
  273.     string oneword(""), initstr("TEMP_INIT");
  274.     char s[11], *f;
  275.     int n= 0;
  276.     strcpy(s, "TEMP2");
  277.     for (int i=0;i<10;i++)
  278.     {
  279.         oneword = s;
  280.         //oneword += onenum;
  281.         if (i>4)
  282.         {
  283.             strcpy(s,"Last 5"); 
  284.              n = i*100;              
  285.         }
  286.         else
  287.         {
  288.             strcpy(s,"First 5");
  289.             n = i*10;
  290.         }
  291.         instMem.Store(oneword, i);
  292.         dataMem.Store(n, i);
  293.         
  294.     }
  295.     cout << "FROM INIT:" << endl;
  296.     cout << "intsMem: ";
  297.     for (int j=0;j<10;j++)
  298.         cout << instMem.Fetch(j) << " ";
  299.     cout << endl;
  300.     cout << "dataMem: ";
  301.     for (int k=0;k<10;k++)
  302.         cout << dataMem.Fetch(k) << " ";
  303.     cout << endl;
  304.     instMem.Store(initstr,5);  
  305.     /* the preceding line will not cause all cells in instMem to 
  306. contain the value of initstr?  I have no idea why*/
  307.     
  308.     return TRUE;
  309. }
  310.  
  311.  
  312. void VMPROC::DisplayMem()
  313. {
  314.     cout << "\nFROM DISPLAYMEM: "<< endl;
  315.     cout << "instMem: \n";
  316.     for (int i=0;i<100;i++)
  317.         cout << i << " : " << instMem.Fetch(i) << " ";
  318.     cout << endl;
  319.     cout << "dataMem: \n";
  320.     for (int j=0;j<10;j++)
  321.         cout << dataMem.Fetch(j) << " ";
  322.     cout << endl;
  323. }
  324.  
  325.  
  326. int main()
  327. {
  328. VMPROC test;
  329. test.Init();
  330. test.DisplayMem();
  331. return 0;
  332. }
  333.  
  334.  
  335. OUTPUT:
  336.  
  337. VMPROC instantiated
  338. FROM INIT:        
  339. intsMem: Last 5 Last 5 Last 5 Last 5 Last 5 Last 5 Last 5 Last 5 Last 5 
  340. Last 5 
  341.  
  342. *********THE OUPUT SHOULD BE
  343. First 5 First 5 First 5 First 5 First 5 Last 5 Last 5 Last 5 Last 5 Last 
  344. *****************************
  345.  
  346. dataMem: 0 10 20 30 40 500 600 700 800 900 
  347.  
  348. FROM DISPLAYMEM: 
  349. instMem: 
  350. 0 : Last 5 1 : Last 5 2 : Last 5 3 : Last 5 4 : Last 5 5 : TEMP_INIT 6 : 
  351. Last 5 7 : Last 5 8 : Last 5 9 : Last 5 10 :  11 :  12 :  13 :  14 :  15 
  352. :  16 :  17 :  18 :  19 :  20 :  21 :  22 :  23 :  24 :  25 :  26 :  27 
  353. :  28 :  29 :  30 :  31 :  32 :  33 :  34 :  35 :  36 :  37 :  38 :  39 
  354. :  40 :  41 :  42 :  43 :  44 :  45 :  46 :  47 :  48 :  49 :  50 :  51 
  355. :  52 :  53 :  54 :  55 :  56 :  57 :  58 :  59 :  60 :  61 :  62 :  63 
  356. :  64 :  65 :  66 :  67 :  68 :  69 :  70 :  71 :  72 :  73 :  74 :  75 
  357. :  76 :  77 :  78 :  79 :  80 :  81 :  82 :  83 :  84 :  85 :  86 :  87 
  358. :  88 :  89 :  90 :  91 :  92 :  93 :  94 :  95 :  96 :  97 :  98 :  99 
  359. :  
  360. dataMem: 
  361. 0 10 20 30 40 500 600 700 800 900 
  362. VMPROC destroyed 
  363.  
  364. If you got this far thanks again for your time.
  365.